package com.heima.tlias.controller;

import com.heima.tlias.controller.request.StudentQueryRequest;
import com.heima.tlias.pojo.Student;
import com.heima.tlias.service.StudentService;
import com.heima.tlias.utils.PageBean;
import com.heima.tlias.utils.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Administrator
 * @Date 2024/5/4
 **/
@RestController
@RequestMapping(path = "/students")
@RequiredArgsConstructor
public class StudentController {

    private final StudentService studentService;

    /**
     * 根据条件分页查询学生信息
     *
     * @param request
     * @return
     */
    @GetMapping
    public Result<PageBean<Student>> queryByPage(StudentQueryRequest request) {
        PageBean<Student> pageBean = studentService.queryByPage(request);
        return Result.success(pageBean);
    }

    /**
     * 新增学生
     *
     * @param student
     * @return
     */
    @PostMapping
    public Result<String> insert(@RequestBody Student student) {
        studentService.insert(student);
        return Result.success("操作成功");
    }

    /**
     * 根据id查询学生详情
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result<Student> queryById(@PathVariable("id") Integer id) {
        Student student = studentService.queryById(id);
        return Result.success(student);
    }

    @PutMapping
    public Result<String> updateById(@RequestBody Student student) {
        if (student.getId() == null) {
            return Result.error("缺少参数");
        }
        studentService.updateById(student);
        return Result.success("操作成功");
    }

    /**
     * 批量删除学生信息
     *
     * @param ids
     * @return
     */
    @DeleteMapping(path = "/{ids}")
    public Result<String> deleteByIds(@PathVariable("ids") List<Integer> ids) {
        studentService.deleteByIds(ids);
        return Result.success("操作成功");
    }

    /**
     * 学生违纪扣分
     *
     * @param id
     * @param score
     * @return
     */
    @PutMapping(path = "/violation/{id}/{score}")
    public Result<String> violation(@PathVariable("id") Integer id, @PathVariable("score") Integer score) {
        studentService.violation(id, score);
        return Result.success("操作成功");
    }

}
